import App from './App'
import Vue from 'vue'
import navBar from './components/navBar.vue'
import tabBar from './components/tabbar.vue'
import config from './utils/config'
import uviewui from 'uview-ui'
import {
	request,
	img_url
} from './utils/request.js'
import {
	getUser
} from './utils/getUserInfo'
import store from './store';
import {
	title
} from 'process'

// 状态栏信息
let systemInfo = uni.getSystemInfoSync();
//胶囊信息
let menuButtonInfo = {};
// #ifdef MP-WEIXIN
menuButtonInfo = uni.getMenuButtonBoundingClientRect();
// #endif
// navbarInfo
Vue.prototype.$navBarInfo = {
	statusBarHeight: systemInfo.statusBarHeight,
	navBarHeight: menuButtonInfo.height + (menuButtonInfo.top - systemInfo.statusBarHeight) * 2,
	navBarWidth: menuButtonInfo.left - (systemInfo.windowWidth - menuButtonInfo.left - menuButtonInfo.width),
	navBarContentWidth: menuButtonInfo.left - (systemInfo.windowWidth - menuButtonInfo.left),
}

Vue.config.productionTip = false;
Vue.prototype.$request = request;
Vue.prototype.$store = store;
Vue.prototype.$getUser = getUser;
Vue.prototype.$img_url = img_url;


Vue.component('nav-bar', navBar)
Vue.component('tab-bar', tabBar)

// 导入并挂载全局的分享方法


// 获取位置信息
Vue.prototype.$getLocation = function() {
	uni.removeStorageSync('selection');
	uni.removeStorageSync('selectList');
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			success: function(res) {
				uni.setStorageSync('location', {
					longitude: res.longitude,
					latitude: res.latitude
				})
				resolve(res)
			},
			fail(e) {
				reject(e)
			}
		})
	})
}
Vue.prototype.$getLocation = function() {
	uni.removeStorageSync('selection');
	uni.removeStorageSync('selectList');
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			success: function(res) {
				uni.setStorageSync('location', {
					longitude: res.longitude,
					latitude: res.latitude
				})
				resolve(res)
			},
			fail(e) {
				reject(e)
			}
		})
	})
}
Vue.prototype.$rangeObj = function(cityName) {
	this.$request({
		url: 'wedding/show_type',
		data: {
			city: cityName
		}
	}).then(res => {
		if (res.data.code === 1) {
			let obj = res.data.data;
			for (let key in obj) {
				obj[key] = obj[key] == 1 ? 0 : 1
			}
			this.$store.commit('rangeObj', JSON.parse(JSON.stringify(obj)));
		}
	})
}

// 图片上传
Vue.prototype.$upLoadImage = function() {
	let userInfo = uni.getStorageSync("userInfo")
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 1,
			success(res) {
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: "https://sz.vvv5g.com/api/common/upload",
					name: "file",
					header: {
						'Content-Type': 'application/json',
						'token': userInfo.token
					},
					filePath: res.tempFilePaths[0],
					formData: {
						'user': 'test'
					},
					success(ures) {
						let dres = JSON.parse(ures.data)
						if (dres.code === 1) {
							resolve(dres.data)
							uni.hideLoading()
						} else {
							reject(dres.msg)
						}
					},
					fail(e) {
						reject("上传失败请重试")
						uni.hideLoading()
					}
				})
			},
			fail: res => {
				return false
			}
		})
	})
}
// 图片上传-列表
Vue.prototype.$upLoadImages = function() {

	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 9,
			success(res) {
				uni.showLoading({
					title: '上传中'
				});
				let promiseArr = [];
				for (let i = 0; i < res.tempFilePaths.length; i++) {
					promiseArr.push(uploadImgs(res.tempFilePaths[i]));
				}
				Promise.all(promiseArr).then(res => {
					resolve(res);
					uni.hideLoading();
				})
			},
			fail: res => {
				return false
			}
		})
	})
}

function uploadImgs(filePath) {
	let userInfo = uni.getStorageSync("userInfo")
	return new Promise((res, rej) => {
		uni.uploadFile({
			url: "https://sz.vvv5g.com/api/common/upload",
			name: "file",
			header: {
				'Content-Type': 'application/json',
				'token': userInfo.token
			},
			filePath,
			formData: {
				'user': 'test'
			},
			success(ures) {
				let dres = JSON.parse(ures.data, )
				if (dres.code === 1) {
					res(dres.data.fullurl)
				} else {
					rej(dres.msg)
				}
			},
			fail(e) {
				reject("上传失败请重试")
				uni.hideLoading()
			}
		})
	})
}
// 判断苹果手机屏下线
Vue.prototype.bottomNav = function() {
	let system = uni.getSystemInfoSync()
	let phone = system.model.split(" ")
	if (phone[0] == "iPhone") {
		let text = phone[1].slice(0, 1)
		switch (text) {
			case '5':
			case '6':
			case '7':
			case '8':
				return false
				break;
			default:
				return true
		}
	} else {
		return false
	}
}

// 时间戳转换时间格式
Vue.prototype.get_date = function(time = 0) {
	time = Number(time) * 1000
	let date = new Date(time)
	let Y = Number(date.getFullYear()),
		M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1,
		D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(),
		h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
		m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
		s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
	// return `${Y}-${M}-${D} ${h}:${m}:${s}`
	return `${Y}-${M}-${D}`
}

Vue.prototype.get_dates = function(time = 0) {
	time = Number(time) * 1000
	let date = new Date(time)
	let Y = Number(date.getFullYear()),
		M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1,
		D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(),
		h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
		m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
		s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
	return `${Y}-${M}-${D} ${h}:${m}:${s}`
}
Vue.prototype.get_datesForDian = function(time = 0) {
	time = Number(time) * 1000
	let date = new Date(time)
	let Y = Number(date.getFullYear()),
		M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1,
		D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(),
		h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
		m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
		s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
	return `${Y}.${M}.${D} ${h}:${m}:${s}`
}
Vue.prototype.get_datem = function(time = 0) {
	time = Number(time) * 1000
	let date = new Date(time)
	let Y = Number(date.getFullYear()),
		M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1,
		D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(),
		h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
		m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
	return `${Y}.${M}.${D} ${h}:${m}`
}
// 获取当前月有多少天
Vue.prototype.getMonthDay = function(year, month) {
	let days = new Date(year, month, 0).getDate()
	return days
}
// 登录
Vue.prototype.loagin = function() {
	uni.login({
		success: function(res) {
			getUser(res)
		},
		fail() {
			uni.showToast({
				title: '登录失败',
				icon: 'none',
				duration: 2000
			})
			uni.navigateBack({})
		}
	})
}

// 刷新用户数据
// 用户身份 1 =》注册会员 2 =》返利会员   3 =》商家职员   4 =》合伙人
Vue.prototype.$getUserInfo = function() {
	uni.showLoading({
		title: '刷新中'
	});
	request({
		url: 'user/refresh',
		method: 'POST'
	}).then(res => {
		if (res.statusCode == 200) {
			uni.hideLoading();
			store.commit("setRecordData", res.data.data.statistics);
			store.commit("setData", res.data.data.userinfo);
			store.commit("setIsLogin", true);
			store.dispatch('getCarList');
			store.dispatch('getCollection')
			store.dispatch('getBrowse')
		}
		if (res.data.code == 401) {
			uni.hideLoading();
			uni.showToast({
				title: '请登录后操作',
				icon: 'none',
				duration: 2000,
			})
			uni.navigateTo({
				url: "../user/login"
			})
		}
	})
}

// 调起微信支付
Vue.prototype.$wx_pay = function(config) {
	uni.showLoading({
		title: "支付调起中..."
	})
	return new Promise((resolve, reject) => {
		uni.hideLoading()
		uni.requestPayment({
			provider: "wxpay",
			...config,
			success(res) {
				resolve(res)
			},
			fail(e) {
				reject(e)
			},
			complete(e) {

			}
		})
	})
}
// 返回上一页并且可以执行上一页的方法
Vue.prototype.prePage = function(num = 2) {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - num];
	return prePage.$vm;
}
// 展示规格显示的位置
Vue.prototype.spceType = function(title, skuText = "", isScpe) {
	let newTitle = title
	if (isScpe == '1') {
		newTitle = skuText + '  ' + title
	} else if (isScpe == '2') {
		newTitle = title + '  ' + skuText
	}
	return newTitle
}


const config1 = (name) => {
	return config[name];
}

// url 提示标题，不需要OSS处理
const appstc = (url) => {
	return config.cdnurl + '/assets/addons/wanlshop/img' + url;
}

// multiple  放大倍数  resize  resize  format  图形格式 type  使用类型  modenum 1.自适应 2.固定宽高短边缩放 3.固定宽高居中裁剪 4.强制宽高
// w  宽度  h  高度url 图像地址

const oss = (url) => {
	let image = '';
	let mode = ["m_lfit", "m_mfit", "m_fill", "m_fixed"];
	// 图像，自适应方向：1，渐进显示：1，转换格式：jpg，图片质量：q_90，图片锐化：50，浓缩图
	let rule = ["?x-oss-process=image", "auto-orient,1", "interlace,1", "format,jpg", "quality,q_90", "sharpen,50",
		"resize,m_fixed,w_120,h_120"
	];
	// 当地址不存在
	if (url) {
		if ((/^data:image\//.test(url))) {
			image = url;
		} else if ((/^(http|https):\/\/.+/.test(url))) {
			rule.pop();
			image = url + rule.join("/");
		} else {
			image = config.cdnurl + url + rule.join("/");
		}
	} else {
		return false
	}
	return image;
}

// 提示信息
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
	// 震动
	// uni.vibrateShort();
}
Vue.use(uviewui)
Vue.prototype.$wanlshop = {
	appstc,
	oss,
	config1,
	msg
};

App.mpType = 'app'
const app = new Vue({
	store,
	...App
})
app.$mount()